# -*- coding: utf-8 -*-
# @Time    : 2022/3/14 10:11
# @Author  : TangDagui
# @Email   : 3297364807@qq.com
# @File    : ImageDown.py
# @Software: PyCharm
import re
import os
import urllib.request
import requests
import cv2
import xlrd


def downLoad(username):
    url = "https://q1.qlogo.cn/g?b=qq&nk=" + username + "&s=640"  # 下载照片的url
    file_path = "image/"
    file = file_path + username + ".png"
    # 打开文件准备写入
    r = requests.get(url)
    if r.status_code == 200:  # 接口响应200才进行照片下载
        with open(file, "wb") as f:
            for chunk in r.iter_content(128):  # 每到128位就写入
                f.write(chunk)
    pass


def clickOpenCv(username):
    image = cv2.imread("image/" + username + ".png")
    image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    face_cascade = cv2.CascadeClassifier("xml/haarcascade_frontalface_alt2.xml")
    # 检测图像中的所有人脸
    faces = face_cascade.detectMultiScale(image_gray)
    if len(faces) > 0:
        cv2.imwrite("image2/" + username + ".png", image)
    pass


if __name__ == '__main__':
    data = xlrd.open_workbook_xls(r'data.xls')
    tables = data.sheets()[0]
    # for i in tables.col_values(0):
    #     downLoad(i[:-1])
    for i in tables.col_values(0):
        clickOpenCv(i[:-1])

# downLoad()
